package com.relytosoft.mizar.media.display.services.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSON;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.dubhe.common.redis.utils.RedisUtil;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.common.constant.MediaCommonConstants;
import com.relytosoft.mizar.media.display.api.entity.VideoConfig;
import com.relytosoft.mizar.media.display.services.service.VideoConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 视频预览配置控制器
 *
 * @author hj
 * @version 1.0
 * @date 2022/4/13 16:14
 * @ClassName VideoConfigController
 */
@Validated
@RestController
@RequestMapping("display/videoConfig")
@Api(tags = "视频预览配置")
public class VideoConfigController {

    @Resource
    private VideoConfigService videoConfigService;

    @ApiOperation(value = "查询当前用户的配置")
    @GetMapping()
    @SaCheckPermission("display:videoConfig:get")
    @OpLog(msg = "查询当前用户配置",opType = OpTypeEnum.SELECT)
    public ResponseResult<VideoConfig> getConfig(){
        return ResponseResult.ok(videoConfigService.getByUserId());
    }

    @ApiOperation(value = "编辑当前配置")
    @PostMapping()
    @SaCheckPermission("display:videoConfig:update")
    @OpLog(msg = "编辑配置",opType = OpTypeEnum.UPDATE)
    public ResponseResult<Void> editConfig(@RequestBody @Valid VideoConfig config){
        videoConfigService.edit(config);
        RedisUtil.set(MediaCommonConstants.CACHE_USERVIDEOCONFIG + CurrentUser.getCurrentUserId(), JSON.toJSONString(config),36000L);
        return ResponseResult.ok();
    }
}
